using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using BrightIdeasSoftware;

namespace VirtualObjectListViewDemo
{
    public partial class MainForm : Form
    {
        private VirtualObjectListView _vOlv;
        private List<LogEntry> _logEntries = new List<LogEntry>();

        public MainForm()
        {
            InitializeComponent();
            InitializeVirtualListView();
            GenerateSampleData(100000); // 生成10万条示例数据
            BindData();
        }

        // 初始化VirtualObjectListView
        private void InitializeVirtualListView()
        {
            // 创建控件并设置基本属性
            _vOlv = new VirtualObjectListView
            {
                Dock = DockStyle.Fill,
                View = View.Details,
                FullRowSelect = true,
                GridLines = true,
                // 虚拟模式特有属性
                VirtualListSize = 0, // 初始无数据
               // CacheVirtualItems+= // 启用缓存提升性能
                // 其他通用属性
                //AllowColumnReordering = true,
                UseAlternatingBackColors = true,
                RowHeight = 22,
                //cACHE
               
            };

            // 配置列（绑定到LogEntry属性）
            _vOlv.Columns.AddRange(new OLVColumn[]
            {
                new OLVColumn("ID", "Id") { Width = 80 },
                new OLVColumn("时间", "Timestamp") 
                { 
                    Width = 180,
                    AspectToStringConverter = val => ((DateTime)val).ToString("yyyy-MM-dd HH:mm:ss")
                },
                new OLVColumn("级别", "Level") 
                { 
                    Width = 100,
                    // 根据级别显示不同颜色
                    //CellFormatter = (cell) =>
                    //{
                    //    var level = cell.SubItem.Text;
                    //    cell.SubItem.ForeColor = level switch
                    //    {
                    //        "Error" => Color.Red,
                    //        "Warning" => Color.Orange,
                    //        _ => Color.Green
                    //    };
                    //}
                },
                new OLVColumn("消息", "Message") { Width = 500 }
            });

            // 添加事件：双击查看详情
            _vOlv.DoubleClick += (s, e) =>
            {
                var selected = _vOlv.SelectedObject as LogEntry;
                if (selected != null)
                {
                    MessageBox.Show($"详细信息:\n{selected.Message}", 
                                  $"日志 #{selected.Id}");
                }
            };

            // 添加到窗体
            Controls.Add(_vOlv);
        }

        // 生成大量示例数据
        private void GenerateSampleData(int count)
        {
            var levels = new[] { "Info", "Warning", "Error" };
            var rnd = new Random();
            
            for (int i = 0; i < count; i++)
            {
                _logEntries.Add(new LogEntry
                {
                    Id = i + 1,
                    Timestamp = DateTime.Now.AddMinutes(-i),
                    Level = levels[rnd.Next(levels.Length)],
                    Message = $"这是第 {i + 1} 条日志消息，类型为 {levels[rnd.Next(levels.Length)]}"
                });
            }
        }

        // 绑定数据到VirtualObjectListView
        private void BindData()
        {
            // 关键：设置数据源
            _vOlv.SetObjects(_logEntries);
            
            // 可选：初始排序（按ID降序）
            //_vOlv.Sort(_vOlv.Columns[0], SortOrder.Descending);
        }

        // 设计器代码
        private void InitializeComponent()
        {
            SuspendLayout();
            ClientSize = new Size(900, 600);
            Name = "MainForm";
            Text = "VirtualObjectListView 示例（10万条数据）";
            ResumeLayout(false);
        }
    }

    // 示例数据模型
    public class LogEntry
    {
        public int Id { get; set; }
        public DateTime Timestamp { get; set; }
        public string Level { get; set; } // Info, Warning, Error
        public string Message { get; set; }
    }

    // 程序入口
    //static class Program
    //{
    //    [STAThread]
    //    static void Main()
    //    {
    //        Application.EnableVisualStyles();
    //        Application.SetCompatibleTextRenderingDefault(false);
    //        Application.Run(new MainForm());
    //    }
    //}
}
